﻿using Bussiness.DEV;
using HslCommunication.ModBus;
using Model;
using System;
using System.Collections.Generic;

namespace Bussiness
{
    public class GSK:IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        private ModbusTcpNet _ModbusTcpNet;

        public Result Connect()
        {
            Result result = new Result();
            _ModbusTcpNet = new ModbusTcpNet();
            _ModbusTcpNet.IpAddress = Device.IP;
            _ModbusTcpNet.Port = Device.Port.Value;
            _ModbusTcpNet.ConnectTimeOut = 3000;
            var operateResult = _ModbusTcpNet.ConnectServer();
            result.Success = operateResult.IsSuccess;
            result.Msg = operateResult.Message;
            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            result.Success = true;
            result.Data = new List<Cfg_Field>();
            //读取点位
            foreach (var devPOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devPOI.DevPOIAlias;
                switch ((DevPOIType)devPOI.DevPOIType)
                {
                    case DevPOIType.data_int:
                        var operateResult32 = _ModbusTcpNet.ReadInt32(devPOI.Address);
                        if (operateResult32.IsSuccess)
                        {
                            cfg_Field.value = operateResult32.Content.ToString();
                        }
                        else
                        {
                            result.Success = false;
                        }
                        
                        break;
                    case DevPOIType.data_short:
                        var operateResult16 = _ModbusTcpNet.ReadUInt16(devPOI.Address);
                        if (operateResult16.IsSuccess)
                        {
                            cfg_Field.value = operateResult16.Content.ToString();
                        }
                        else
                        {
                            result.Success = false;
                        }
                        break;
                    case DevPOIType.data_long:
                        var operateResult64 = _ModbusTcpNet.ReadInt64(devPOI.Address);
                        if (operateResult64.IsSuccess)
                        {
                            cfg_Field.value = operateResult64.Content.ToString();
                        }
                        else
                        {
                            result.Success = false;
                        }
                        break;
                    case DevPOIType.data_float:
                        var operateResultFloat = _ModbusTcpNet.ReadFloat(devPOI.Address);
                        if (operateResultFloat.IsSuccess)
                        {
                            cfg_Field.value = operateResultFloat.Content.ToString();
                        }
                        else
                        {
                            result.Success = false;
                        }
                        break;
                    case DevPOIType.data_string:
                        string[] arr = devPOI.Address.Split('_');
                        ushort length = Convert.ToUInt16(arr[1]);
                        var operateResultString = _ModbusTcpNet.ReadString(devPOI.Address, length);
                        if (operateResultString.IsSuccess)
                        {
                            cfg_Field.value = operateResultString.Content.ToString();
                        }
                        else
                        {
                            result.Success = false;
                        }
                        break;
                }

                result.Data.Add(cfg_Field);
            }

            return result;
        }
    }
}
